/**
 * ServiceException.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月14日
 */
package com.pshare.framework.exception;

import com.pshare.enums.CodeMsg;

/**
 * 自定义服务层Exception
 */
public class SeaxRuntimeException extends RuntimeException {

	private static final long serialVersionUID = 3583566093089790852L;
	private Integer code = CodeMsg.BIZ_ERROR.getCode();
	private String msgCn = CodeMsg.BIZ_ERROR.getMessage();
	private String msgEn = CodeMsg.BIZ_ERROR.getEnglishMsg();
	private static final String MSG = CodeMsg.BIZ_ERROR.getMessage();

	public SeaxRuntimeException() {
		 super(MSG);
	}
	
	public SeaxRuntimeException(Integer code) {
		this.code = code;
		this.msgCn = CodeMsg.getMsg(code, false);
		this.msgEn = CodeMsg.getMsg(code, true);
	}
	
	public SeaxRuntimeException(CodeMsg codeMsg) {
		this.code = codeMsg.getCode();
		this.msgCn = codeMsg.getMessage();
		this.msgEn = codeMsg.getEnglishMsg();
	}

	public SeaxRuntimeException(String message) {
		super(message);
	}

	public SeaxRuntimeException(String msgCn, String msgEn) {
		super(msgCn);
		this.msgEn = msgEn;
	}

	public SeaxRuntimeException(Throwable cause) {
		super(cause);
	}

	public SeaxRuntimeException(String message, Throwable cause) {
		super(message, cause);
	}
	
	public SeaxRuntimeException(String message, String msgEn, Throwable cause) {
		super(message, cause);
		this.msgEn = msgEn;
	}

	public Integer getCode() {
		return code;
	}

	public String getMsgEn() {
		return msgEn;
	}
	
	/**
	 * 根据中英文环境返回不同的信息
	 * @param isEN 是否英文
	 * @return String
	 */
	public String getMsg(Boolean isEN) {
		if (isEN == null || !isEN) {
			return this.msgCn == null ? getMessage() : msgCn;
		} else {
			return getMsgEn();
		}
	}
	
	public String getMsg() {
		return this.msgCn == null ? getMessage() : msgCn;
	}

	/**
	 * 正式环境重写此方法 测试环境不重写 抛出栈 定位具体类跟代码行 重写fillInStackTrace 性能开销在于: 1.
	 * 是一个synchronized方法(主因) 2. 需要填充线程运行堆栈信息
	 */
	@Override
	public Throwable fillInStackTrace() {
		return this;
	}

}
